package com.company.crm.settings.web.controller;

import com.company.crm.commons.constants.Constants;
import com.company.crm.commons.domain.ReturnObject;
import com.company.crm.commons.util.DateUtils;
import com.company.crm.commons.util.MD5Util;
import com.company.crm.settings.domain.User;
import com.company.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：王磊栋
 * 2021/4/10
 */
//登录页面
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("/settings/qx/user/toLogin")
    public String toLogin(HttpServletRequest request){
        //取出cookie
        Cookie[] cookies = request.getCookies();
        if (cookies.length>1) {
            String loginAct = null;
            String loginPwd = null;
            for (Cookie cookie : cookies) {
                if ("loginAct".equals(cookie.getName())) {
                    loginAct = cookie.getValue();
                }
                if ("loginPwd".equals(cookie.getName())){
                    loginPwd = cookie.getValue();
                }
            }
            if (loginAct!=null&&loginPwd!=null){
                //证明值了，但是为了防止中间修改密码那么就再次进行数据库的验证
                Map<String,Object> map = new HashMap<>();
                map.put("loginAct", loginAct);
                map.put("loginPwd", loginPwd);
                User user = userService.selectUserByUserNameAndPass(map);
                //一样把这个user对象放到session作用域中，后面用对象的名字
                if (user!=null){
                    request.getSession().setAttribute(Constants.SESSION_USER, user);
                    //并且直接跳转到工作台
                    return "workbench/index";
                }

            }
        }
        return "settings/qx/user/login";
    }

    @RequestMapping("/settings/qx/user/login")
    @ResponseBody
    public ReturnObject login(String loginAct, String loginPwd, String isRemPwd, HttpSession session,HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        map.put("loginAct",loginAct);
        map.put("loginPwd", MD5Util.getMD5(loginPwd));
        User user = userService.selectUserByUserNameAndPass(map);
        ReturnObject returnObject = new ReturnObject();
        if (user==null){
            returnObject.setCode(Constants.LOGIN_CODE_FAIL);
            returnObject.setMessage("密码或用户名不正确！");
            return returnObject;
        }else{
            if (!"1".equals(user.getLockState())){
                returnObject.setCode(Constants.LOGIN_CODE_FAIL);
                returnObject.setMessage("账号已经被锁，请联系管理员！");
                return returnObject;
            }else if(DateUtils.formatDateTime(new Date()).compareTo(user.getExpireTime())>0){
                returnObject.setCode(Constants.LOGIN_CODE_FAIL);
                returnObject.setMessage("账号已经过期！");
                return returnObject;
            }
            //成功
            returnObject.setCode(Constants.LOGIN_CODE_SUCCESS);
            //因为需要在工作台的右上角需要显示用户的姓名。
            session.setAttribute(Constants.SESSION_USER, user);
            if ("true".equals(isRemPwd)){
                //成功之后，把密码和用户名保存在cookie中
                Cookie cookie1 = new Cookie("loginAct",loginAct);
                Cookie cookie2 = new Cookie("loginPwd",MD5Util.getMD5(loginPwd));
                cookie1.setMaxAge(10*24*60*60);
                cookie2.setMaxAge(10*24*60*60);
                response.addCookie(cookie1);
                response.addCookie(cookie2);
            }
            return  returnObject;
        }

    }
    @RequestMapping("/workbench/index.do")
    public String workbenchLogin(){
        return "workbench/index";
    }

    @RequestMapping("/settings/qx/user/logout.do")
    public String logout(HttpSession session, HttpServletResponse response){
        //需要把session作用域清空，并且把cookie清空
        session.invalidate();
        Cookie cookie1 = new Cookie("loginAct",null);
        Cookie cookie2 = new Cookie("loginPwd",null);
        cookie1.setMaxAge(0);
        cookie2.setMaxAge(0);
        response.addCookie(cookie1);
        response.addCookie(cookie2);
        return "redirect:/";
    }
}
